package com.leps.nq.common.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;

/**
 * API统一返回给前端的数据结构
 */
@ApiModel(description = "统一返回对象")
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L; // 序列化版本号

    public static final int CODE_SUCCESS = 200; // 成功状态码
    public static final int CODE_ERROR = 500; // 错误状态码
    public static final int CODE_WARNING = 501; // 警告状态码
    public static final int CODE_UNAUTHORIZED = 403; // 无权限状态码
    public static final int CODE_UNAUTHENTICATED = 401; // 未登录状态码
    public static final int CODE_INVALID_REQUEST = 400; // 无效请求状态码

    @ApiModelProperty("结果码：200 OK, 500 Error, 501 Warning, 400 Bad Request, 401 Not Authorized, 403 Forbidden")
    public int code; // 状态码
    @ApiModelProperty("结果描述")
    public String msg; // 描述信息
    @ApiModelProperty(required = false, value = "业务数据")
    public T data; // 携带对象

    public R(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public R(int code, String msg) {
        this(code, msg, null);
    }

    public int getCode() {
        return this.code;
    }

    public R<?> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public String getMsg() {
        return this.msg;
    }

    /**
     * 给data赋值，连缀风格
     */
    public R<T> setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        String data_string = null;
        if (data == null) {

        } else if (data instanceof List) {
            data_string = "List(length=" + ((List<?>) data).size() + ")";
        } else {
            data_string = data.toString();
        }
        return "{" + "\"code\": " + this.getCode() + ", \"msg\": \"" + this.getMsg() + "\"" + ", \"data\": "
                + data_string + "}";
    }

}
